package com.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //完成主要的业务逻辑

        //直接向页面输出内容
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        //(1)获取来自login.jsp页面上用户名和密码
        String uname = req.getParameter("uname");//获取用户名的数据
        String pwd = req.getParameter("pwd");//获取密码的数据
        //(2)判断空值
        if (uname !=null && uname.length()>0 && pwd !=null && pwd.length()>0) {
            //判断用户名是否是abc 密码是否为123
            if (uname.equals("abc") && pwd.equals("123")) {
//                resp.sendRedirect("info.jsp");//直接跳转到jsp
                out.println(uname +"：欢迎你，你真棒!!");
            } else {
                out.println("用户名并非abc，或者密码不是123");
            }
        } else {
            out.println("用户名或密码为空");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
